﻿using System;
using System.IO;
using System.Linq;
using NUnit.Framework;

namespace SvnDeploy.Service.Impl
{
    [TestFixture]
    public class FileSystemImplTests
    {
        private IFileSystem fileSystem;
        private static readonly string newDirectory = @"C:\this is a test directory " + Guid.NewGuid();
        private string deleteFilePath;
        private string dummyFilePath;

        [SetUp]
        public void BeforeEachTest()
        {
            deleteFilePath = newDirectory + @"\delete_this_file.txt";
            dummyFilePath = newDirectory + @"\dummy_file.txt";
            fileSystem = new FileSystemImpl();
            Directory.CreateDirectory(newDirectory);
            CreateFile(deleteFilePath);            
            CreateFile(dummyFilePath);
        }        

        [TearDown]
        public void AfterEachTest()
        {
            Directory.Delete(newDirectory, true);
        }

        [Test]
        public void CreateDirectory_ShouldCreateDirectory()
        {
            string path = newDirectory + @"\test dir";
            fileSystem.CreateDirectory(path);
            Assert.IsTrue(Directory.Exists(path));            
        }

        [Test]
        public void DeleteFile_ShouldRemoveFile()
        {
            fileSystem.DeleteFile(deleteFilePath);
        }

        [Test]
        public void DoesDirectoryExist_WhenDirectoryExists_ShouldReturnTrue()
        {
            fileSystem.DoesDirectoryExist(newDirectory);
        }

        [Test]
        public void GetFiles_ShouldReturnFiles()
        {
            var files = fileSystem.GetFiles(newDirectory);
            Assert.NotNull(files);
            Assert.IsTrue(files.Count() > 0);
        }

        [Test]
        public void IsDirectoryEmpty_WhenDirectoryNotEmpty_ShouldReturnFalse()
        {            
            Assert.IsFalse(fileSystem.IsDirectoryEmpty(newDirectory));
        }

        private static void CreateFile(string path)
        {
            using (var file = new FileStream(path, FileMode.OpenOrCreate))
            {
                using (var writer = new StreamWriter(file))
                {
                    writer.Write("This is some sample text.");
                }
            }
        }
    }
}
